<?php
namespace app\bis\controller;
use think\Controller;
class Register extends Controller{
   
    public function  index(){
        //获取城市的一级栏目
        $citys=model("City")->getNormalFirstCitys();
        //获取服务类别栏目
        $categorys=model("Category")->getNormalFirstCategorys();
        return  $this->fetch('',[
            'citys'=>$citys,
            'categorys'=>$categorys,
        ]);
    }
    //数据过滤以及数据添加
    public function add(){
        //判断请求
        if(!Request()->isPost()){
            $this->error("非法请求");
        }
       //验证数据
        $data=input("post.");
        $validate=validate("Bis");
        if(!$validate->scene("add")->check($data)){
            return $this->error($validate->getError());
        }
        //获取经纬度
        $lnglat= \Map::getLngLat($data['address']);
        if(empty($lnglat) || $lnglat['status'] !=0 ||  $lnglat['result']['precise'] !=1){
            $this->error("无法获取数据 或者 数据不准确");
        }
        
       // 验证所提交的用户有没有存在
        $accountResult=model("BisAccount")->get(["username"=>$data['username']]);
        if($accountResult){
            $this->error("用户已存在");
        }
        //商户基本信息入库
        $bisData=[
            "name"=>$data['name'],
            "city_id"=>$data['city_id'],
            "city_path"=>empty($data['se_city_id']) ?$data['city_id']:$data['city_id'].','.$data['se_city_id'],
            "logo"=>$data['logo'],
            "licence_logo"=>$data["licence_logo"],
            "description"=>empty($data["description"]) ? "":$data["description"],
             'bank_info' =>  $data['bank_info'],
             'bank_user' =>  $data['bank_user'],
             'bank_name' =>  $data['bank_name'],
             'faren' =>  $data['faren'],
             'faren_tel' =>  $data['faren_tel'],
             'email' =>  $data['email'],
        ];
        
        $bisId=model("Bis")->add($bisData);
        //总店相关信息检验
         $data['cat']="";
         if(!empty($data['se_category_id'])){
             $data['cat']= implode("|",$data['se_category_id']);
         }
        //总店信息相关入库其中$bisId是baseModel add方法中返回的值
         $localtionData=[
             'bis_id'=>$bisId,
             'name' => $data['name'],
             'logo' => $data['logo'],
             'tel' => $data['tel'],
             'bank_info' =>  $data['bank_info'],
             'contact' => $data['contact'],
             'category_id' => $data['category_id'],
             'category_path'=>$data["category_id"].",".$data['cat'],//cat
             'city_id'=>$data['city_id'],
             'city_path'=>empty($data['se_city_id'])?$data['city_id']:$data['city_id'].','.$data['se_city_id'],
             'api_address'=>$data['address'],
             'address'=>$data['address'],
             'open_time'=>$data['open_time'],
             'content'=>empty($data['content'])? '' : $data['content'],
             'is_main'=>1,//代表总店
             'xpoint' =>empty($lnglat['result']['location']['lng']) ? '' : $lnglat['result']['location']['lng'],
             'ypoint' =>empty($lnglat['result']['location'][ 'lat']) ? '' : $lnglat['result']['location']['lat'],
             
         ];
        $localtionId=model("BisLocation")->add($localtionData);
        //账户入库
        $data['code']=mt_rand(100,10000);//加盐
        $accountData=[
            "bis_id"=>$bisId,
            'code'=>$data['code'],
            'username'=>$data["username"],
            'password'=>md5($data['password'].$data['code']),
            'is_main'=>1,//代表的是总管理员
            
        ];
        //店铺管理员入库
        $accountId=model("BisAccount")->add($accountData);
        if(!$accountId){
            $this->error("申请失败");
        }
        //发送邮箱
        $url=request()->domain().url('bis/register/waiting',['id'=>$bisId]);
        $title = "o2o入驻申请通知";
        $content="您提交的入驻申请需等待平台方审核，您可以通过点击链接<a href='".$url."'   target='_blank'>查看链接</a>审核状态";
        \phpmailer\Email::send($data['email'],$title,$content);
        
        $this->success("申请成功",url('bis/register/waiting',['id'=>$bisId]));
    }
    
    public function waiting($id){
        if(empty($id)){
            $this->error("error");
        }
        $detail=model("Bis")->get($id);
        
        return $this->fetch("",[
            'detail'=>$detail,
        ]);
        
        
    }
    


}
